// 引入路由模块
const { Router } = require('express')
// 创建一个路由实例
const router = new Router()

// 引入用户信息表模型
const userImformationModel = require('../../model/userImformation')

// 引入写好的操作对象方法
const {findOneDoc} = require('../../utils/DB/controlDB')

// 引入jwt生成模块
const {generateToken, overToken, verifyToken} = require('../../utils/verification')


router.post('/login',(request, response)=>{
    const { user, password } = request.body
    const docObJ = {
        model: userImformationModel,
        option: {
            count: user,
            password
        },
        attachOption: {
            // _id:0,
            count:1,
            roles:1
        },
        callback: function (data) {
            const mydata = data
            let todata;
            if (!data.data && typeof data.data != "undefined" && data.data != 0){
                todata = {
                    code: '200',
                    data: null,
                    msg: '该用户未注册'
                }
            } else {
                const token = generateToken(mydata.data)
                todata = {
                    code: '200',
                    data: {
                        _id: mydata.data._id,
                        count: mydata.data.count,
                        token
                    },
                    msg: 'success'
                }
            }

            response.send(JSON.stringify(todata))
        }
    }
    findOneDoc(docObJ)
})

module.exports = router
